﻿using System;
using System.Windows.Data;
using System.Windows;
using BizControl.Library.BusinessObjects.Activities;
using System.Windows.Shapes;
using BizControl.Library.BusinessObjects.Marks;

namespace BizControl.Presentation.ActivityEditModule.Converters
{
  public class ConditionTypeToElementStatePathConverter : IMultiValueConverter
  {
    private readonly int ConditionTypeIndex = 0;
    private readonly int ElementIndex = 1;

    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      var type = (ConditionType)Enum.Parse(typeof(ConditionType), values[ConditionTypeIndex].ToString());
      Path path = new Path();
      switch (type)
      {
        case ConditionType.MarkRequired:
          if ((values[ElementIndex] as MarkInfo) != null)
          {
            var state = (values[ElementIndex] as MarkInfo).State;
            if (state.HasFlag(MarkState.Completed)) path = (Path)Application.Current.FindResource("OkPath");
            if (state.HasFlag(MarkState.NotCompleted)) path = (Path)Application.Current.FindResource("CancelPath");
            if (state.HasFlag(MarkState.Overdue)) path = (Path)Application.Current.FindResource("CancelPath");
          }
          break;
        case ConditionType.ActivityRequired:
          if ((values[ElementIndex] as ActivityInfo) != null)
          {
            var state = (values[ElementIndex] as ActivityInfo).State;
            if (state.HasFlag(ActivityState.Completed)) path = (Path)Application.Current.FindResource("OkPath");
            if (state.HasFlag(ActivityState.Normal)) path = (Path)Application.Current.FindResource("CancelPath");
            if (state.HasFlag(ActivityState.Overdue)) path = (Path)Application.Current.FindResource("CancelPath");
          }
          break;
        case ConditionType.SkillRequired:
        case ConditionType.Unknown:
        default:
          break;
      }
      return path;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
      return null;
    }

    #endregion
  }
}
